<?php
namespace Api\Event\Thirdservice;

use Common\Dto\BaseDto;

class Thirdservice766Event extends ThirdserviceEvent
{
    const DEFAULT_AVATAR = DOMAIN.'/Public/Home/ui3/images/avatar-mr.png';

    public function _initialize() {
        parent::_initialize();

        $this->vEvent = event('video');
    }

    /**
     * 首页视频数据
     * @author liubo <liubo@ilongyuan.com.cn>
     * $dto
     * timestamp, limit
     */
    public function index(BaseDto $dto = null)
    {
        $map['game_id'] = 8;
        $map['tags'] = 'stargame';
        $timestamp = intval($dto->timestamp);
        $timestamp && $map['create_time'] = ['$lt' => $timestamp];

        $limit = intval($dto->limit) ?: 10;

        $list = $this->vEvent->lists($map, $limit);
        if (empty($list)) {
            return [];
        }

        foreach ($list as &$value) {
            $value = $this->_format_video($value);
        }
        return $list;
    }

    /**
     * 亮眼推荐
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function recommend_video(BaseDto $dto = null)
    {
        $vService = service('video');

        $map['game_id'] = 8;
        $map['tags'] = 'stargame';
        $map['status'] = $vService::STATUS_PASS;
        $map['type'] = $vService::TYPE_NORMAL;
        $map['create_time'] = ['$gte' => NOW_TIME - 3600*24];

        $videos = $vService->where($map)->order('play desc')->limit(50)->getField('id', true);
        if (empty($videos)) {
            return [];
        }
        shuffle($videos);

        $limit = intval($dto->limit) ?: 4;
        $videos = array_slice($videos, 0, $limit);

        foreach ($videos as &$value) {
            $value = $this->_format_video($value);
        }
        return $videos;
    }

    /**
     * 亮眼推荐个人榜
     * stargame用户发布视频数量
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function recommend_user(BaseDto $dto = null)
    {
        $uService = service('users');
        $vService = service('video');
        $users = $uService->where(['sso_channel' => 'stargame', 'status' => 1])->getField('id', true);
        if (empty($users)) {
            return [];
        }
        $counts = [];
        foreach ($users as $key => &$value) {
            $map = [
                'uid' => $value,
                'status' => $vService::STATUS_PASS,
                'type' => $vService::TYPE_NORMAL,
                'tags' => 'stargame',
                'game_id' => 8,
            ];
            $video_count = intval($vService->where($map)->count());
            if (!$video_count) {
                unset($users[$key]);
                continue;
            }
            $value = [
                'uid' => $value,
                'count' => $video_count,
            ];
            $counts[] = $video_count;
        }
        array_multisort($counts, SORT_DESC, $users);

        $limit = intval($dto->limit) ?: 5;

        $users = array_slice($users, 0, $limit);

        $uEvent = event('user');
        foreach ($users as &$value) {
            $user = $uEvent->info($value['uid'], false);
            $value['nickname'] = $user['nickname'];
            $value['avatar'] = $user['avatar'] ?: $this::DEFAULT_AVATAR;
        }
        return $users;
    }

    /**
     * 视频详情
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function detail(BaseDto $dto = null)
    {
        $vid = intval($dto->vid);
        if (empty($vid)) {
            return false;
        }

        return $this->_format_video($vid);
    }

    /**
     * 用户视频列表
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function user(BaseDto $dto = null)
    {
        $uid = intval($dto->uid);
        if (empty($uid)) {
            return false;
        }
        $user = event('user')->info($uid, false);
        if (empty($user) || empty($user['id'])) {
            return false;
        }
        $map['uid'] = $uid;

        $map['game_id'] = 8;
        $map['tags'] = 'stargame';

        $timestamp = intval($dto->timestamp);
        $timestamp && $map['create_time'] = ['$lt' => $timestamp];

        $limit = intval($dto->limit) ?: 10;

        $list = $this->vEvent->lists($map, $limit);

        foreach ($list as &$value) {
            $value = $this->_format_video($value);
        }

        $vService = service('video');
        $map = [
            'uid' => $uid,
            'status' => $vService::STATUS_PASS,
            'type' => $vService::TYPE_NORMAL,
            'tags' => 'stargame',
            'game_id' => 8,
        ];
        $video_count = intval($vService->where($map)->count());
        $data['user'] = [
            'uid' => $uid,
            'nickname' => $user['nickname'],
            'avatar' => $user['avatar'] ?: $this::DEFAULT_AVATAR,
            'count'  => $video_count,
        ];

        $data['videos'] = $list ?: [];
        return $data;
    }

    public function _format_video($id) {
        $video = $this->vEvent->detail($id, 0, false);
        if (empty($video)) {
            return false;
        }
        $field = 'id,title,create_time,play,thumb,pre_uri,uri,nickname,avatar,user_id';

        $uri = DOMAIN . "/api/video/source/id/{$id}.mp4"
                      . "?uri=" . urlencode($video['uri'])
                      . "&appid={$this->app_id}";
        $video = [
            'id' => $video['id'],
            'title' => $video['title'],
            'detail' => $video['detail'],
            'create_time' => $video['create_time'],
            'play' => $video['statis']['play'],
            'thumb' => $video['thumb'],
            'pre_uri' => $video['pre_uri'],
            'uri' => $uri,
            'nickname' => $video['user']['nickname'],
            'avatar' => $video['user']['avatar'] ?: $this::DEFAULT_AVATAR,
            'uid'    => $video['uid'],
        ];
        return $video;
    }
}